package zhou.teamwork10;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import jin.competition.jan11.User;
import zhou.teamwork10.BaseDao;

public class UserController2 {
	
	//---------登陆操作--------
	public boolean login(String username,String pwd){
		
		BaseDao dao=new BaseDao();
		Connection con=dao.getCon();		
		
    	String sql="select username,pwd from user";
    	
    	  try{		
    	  PreparedStatement ps=con.prepareStatement(sql);
    	 
    	  ResultSet rs=ps.executeQuery();
    	  
    	  while(rs.next()){   		 
    		  
    		  if(rs.getString("username").equals(username)&&rs.getString("pwd").equals(pwd)){  //这种实现方法不好，应先判断是否存在该用户
    				return true;
    			}else{
    				return false;
    			}   		     		     		  
   	  }
   	  if(rs!=null){
    		  rs.close();
    	  }
   	  if(ps!=null){
    		ps.close();    		   		
    	  }
    	  if(con!=null){
    		con.close();    		   		
   	  }		
    			
          }catch(SQLException e){
    	     e.printStackTrace();
    	}
    	return true;
    }
    

	//-----------注册新用户-------------
    public boolean register(User u){
	BaseDao dao=new BaseDao();
	Connection con=dao.getCon();
	String sql="insert into user (username,pwd) value (?,?)";
	
	  try{		
	  PreparedStatement ps=con.prepareStatement(sql);
	  ps.setString(1, u.getUserName());
	  ps.setString(2, u.getUserPassword());
	  ps.executeUpdate();
	  if(ps!=null){
		ps.close();    		   		
	  }
	  if(con!=null){
		con.close();    		   		
	  }		
			
      }catch(SQLException e){
	     e.printStackTrace();
	}
	return true;
}
	
	
	
	

}
